import {inject, observer} from "mobx-react";
import {toJS} from 'mobx';
import {WeaAlertPage, WeaDialog, WeaSelect,WeaBrowser, WeaInput, WeaAuth} from 'ecCom';
import CoworkAuth from './CoworkAuth';
import {Row, Col, Button,Table, Icon, Spin, Form, message, Modal} from 'antd';
import Apis from '../../../apis/cowork';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;


@inject("coworkMainStore")
@observer
class CoworkScope extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      stat: {},
      selectedRowKeys: [],
      edit: false
    }
  }
  componentDidMount() {
    this.reload();
  }
  
  render() {
    const {coworkMainStore} = this.props;
    let stat = coworkMainStore.content.id != this.props.itemsVO.id ? this.state.stat : toJS(coworkMainStore.stat);
    let {alist = []} = stat,
      config = toJS(coworkMainStore.config), {authConditions = []} = config;
    return (
      <CoworkAuth itemsVO={this.props.itemsVO} list={alist} title={getLabel(382638, '参与范围')} authConditions={authConditions}
                  submit={this.submit} delete={this.delete}/>
    );
  }

  submit = (params,func)=> {
    Apis.saveorupdateCoworkShare(params).then((result) => {
      if (result.flag) {
        func();
        this.reload();
      } else {
        message.error(getLabel(22620,'保存失败'));
      }
    });
  }
  
  delete = (rows,callback) => {
    if (rows.length == 0) {
      message.info(getLabel(4908, '请选择要删除的内容','note'));
      return;
    }
    const {coworkMainStore} = this.props;
    let that = this;
    Modal.confirm({
      content: getLabel(23271, '确认要删除吗')+'?',
      onOk() {
        coworkMainStore.deleteScope(rows.map((row) => row.shareid), that.props.itemsVO.id, () => {
          that.reload();
          callback();
        });
      }
    })
  }
  
  reload = () => {
    const {coworkMainStore} = this.props;
    let stat = toJS(coworkMainStore.stat), id = coworkMainStore.content.id;
    if (id != this.props.itemsVO.id) {
      Apis.getShareStat({coworkId: this.props.itemsVO.id, type: 'all'}).then((result) => {
        this.setState({
          stat: result
        });
      });
    } else {
      coworkMainStore.getShareConditionStrList();
    }
  }
}

export default CoworkScope;